import axios from 'axios';

const requestTimeout = 30 * 60 * 1000

import { getStorageValue, setStorageValue, tokenName, userInfoStorageName } from '/src/utils'

const service = axios.create({
    timeout: requestTimeout,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
});

service.interceptors.request.use(
    config => {
        try {
            let html = document.documentElement.outerHTML.split(' src="/assets/index.')
            if (html.length > 1) {
                config.headers.clientversion= html[1].split('.js"></script')[0]
            }
        }catch(ex) {
            console.error('fail', ex)
        }

        const sessionid = getStorageValue(tokenName)
        if (sessionid) {
            config.headers[tokenName] = sessionid
        }
        if (config.extra_headers) {
            for(let p in config.extra_headers) {
                config.headers[p] = config.extra_headers[p]
            }
        }
        let data = config.data
        if (data) {
            if ('pageSize' in data) {
                data.page_size = data.pageSize
                delete data.pageSize
                if ('pageNo' in data) {
                  data.page_no = data.pageNo
                  delete data.pageNo
                } else {
                  data.page_no = 1
                }
              }
              if ('timeout' in config.data) {
                config.timeout = config.data.timeout
                delete config.data.timeout
              }
      
              if (sessionid) {
                config.data[tokenName] = sessionid
              }
        }
        return config;
    },
    error => {
        console.error('request error', error)
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            let config = response.config || {}
            if (config.responseType === 'arraybuffer') {
                try {
                    let data = response.data
                    // if (data.byteLength > 10 && data.byteLength < 500) {
                    //     let err = JSON.parse(arrayBufferToString(data))
                    //     const { code, msg } = err
                
                    //     const codeVerificationArray = isArray(successCode)
                    //     ? [...successCode]
                    //     : [...[successCode]]
                    //     // 是否操作正常
                    //     if (codeVerificationArray.includes(code)) {
                    //     } else {
                    //     handleCode(code, msg)
                    //     return Promise.reject(
                    //         '请求异常拦截:' +
                    //         JSON.stringify({ url: config.url, code, msg }) || 'Error'
                    //     )
                    //     }
                    // }
                }catch(ex) {
                }
            
            }
            return response;
        } else {
            return Promise.reject("response.status="+response.status);
        }
    },
    error => {
        return Promise.reject(error);
    }
);

export default service;
